package com.example.test.activity;

import java.util.ArrayList;
import java.util.Vector;

import com.example.test.R;
import com.example.test.R.id;
import com.example.test.R.layout;
import com.example.test.activity.BindAccountActivity.SpinnerSelectedListener;
import com.example.test.dao.DataBaseHelper;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MoreActivity extends Activity {
	private static final String[] choices={"新浪微博","腾讯微博","人人网","网易微博","搜狐微博"};
	private static final String[] codeChoise={"sina","QQ","renn","netease","sohu"};
	private Vector<String> arrChoices=new Vector<String>();
	private ArrayAdapter<String> adapter;
	private Spinner spNowAccount;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more);
		spNowAccount=(Spinner)findViewById(R.id.spNowAccount);
		setOnclickListener();
	}
	

	@Override
	protected void onResume() {
		setNowAccountSpinner();
		super.onResume();
	}


	private void  setNowAccountSpinner()
	{
		arrChoices.clear();
		arrChoices.add("all");
		SQLiteDatabase database=DataBaseHelper.getRedDatabase(getApplicationContext());
		Cursor cursor=  database.query("account",new String[]{"sina_token","qq_token","renn_token","net_token","sohu_token"}, "name=? ", new String[]{GlobalVar.userName}, null, null, null);
		Vector<String> arrData =new Vector<String>();
		arrData.add(GlobalVar.userName);
		if(cursor.getCount()>0)
		    {
		    	cursor.moveToFirst();
		    	int j=0;
		    	
		    	final int arr[]=new int[1];
		    	for(int i=0;i<choices.length;i++)
		    	{
		    		if(cursor.getString(i)!=null&&!cursor.getString(i).trim().equals(""))
		    		{
		    			arrData.add(choices[i]);
		    			arrChoices.add(codeChoise[i]);
		    		}
		    	}
		    }
		    	String []data=arrData.toArray(new String[arrData.size()]);
		//将可选内容与ArrayAdapter连接起来
				adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data);
				
				//设置下拉列表的风格
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				
				//将adapter 添加到spinner中
				spNowAccount.setAdapter(adapter);
				 
				//添加事件Spinner事件监听  
				spNowAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int pos, long arg3) {
						GlobalVar.enWhich=arrChoices.get(pos);
						GlobalVar.changed=true;
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				
				
				
				int index=arrChoices.indexOf(GlobalVar.enWhich);
				spNowAccount.setSelection(index);
				//设置默认值
				spNowAccount.setVisibility(View.VISIBLE);
	}
	private void setOnclickListener()
	{
		Button tvAccountManage =(Button)findViewById(R.id.tvAccountManage);
		tvAccountManage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(MoreActivity.this,BindAccountActivity.class));
				
			}
		});
		
		Button tvDestroyAccount =(Button)findViewById(R.id.tvDestroyAccount);
		tvDestroyAccount.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				SharedPreferences preferences = MoreActivity.this.getSharedPreferences("default", MODE_PRIVATE);
				Editor editor=preferences.edit();
				editor.putString("account", GlobalVar.userName);
				editor.putString("passwd", "");
				editor.commit();
				MoreActivity.this.startActivity(new Intent(MoreActivity.this,MainActivity.class));
				MoreActivity.this.finish();
			}
		});
		
		Button tvFindFri =(Button)findViewById(R.id.tvFindFri);
		tvFindFri.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(MoreActivity.this,FindFriActivity.class));
				
			}
		});
	}
}
